package com.wht.filter;

import com.wht.domain.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wanghaotian
 * time:2022/11/18,0018 17:46
 * description:
 */

@WebFilter(urlPatterns = "/index.jsp")
public class FilterPower implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setContentType("text/html;charset=utf-8");
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin != null) {
            filterChain.doFilter(request, response);
        }else {
            response.getWriter().print("哥,你还没有登录:<br/>" +
                    "<a href='/admin/login.jsp'>点击去登录</a>");
        }
    }

    @Override
    public void destroy() {

    }
}
